<% layout("/layouts/micro.html",{title:"系统定时任务管理"}){ %>
<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-form layui-card-header layuiadmin-card-header-auto">
			<div class="layui-form-item">
				<div class="layui-inline">
					</div>
				<div class="layui-inline">
					<label class="layui-form-label">任务名：</label>
					<div class="layui-input-block">
						<input class="layui-input" name="name" id="name" autocomplete="off">
					</div>
					</div>
				<div class="layui-inline">
					</div>
				<div class="layui-inline">
					</div>
				<div class="layui-inline">
					</div>
				<div class="layui-inline">
					</div>
				<div class="layui-inline">
					</div>
				<div class="layui-inline">
					</div>
				<div class="layui-inline">
					</div>
				<div class="layui-inline">
					</div>
				<div class="layui-inline">
					</div>
				<div class="layui-inline">
					</div>
				<div class="layui-inline">
					</div>
				<div class="layui-inline">
					<button class="layui-btn layuiadmin-btn-useradmin" lay-submit="" lay-filter="LAY-schedule-search">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
					</button>
				</div>
			</div>
		</div>
		<div class="layui-card-body">
			<%if(shiro.hasPermission("sys:schedule:edit")){%>
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layui-btn-normal addschedule_btn"><i class="layui-icon layui-icon-add-1"></i>添加</button>
                <button class="layui-btn layui-btn-danger delAll_btn"><i class="layui-icon layui-icon-delete"></i>批量删除</button>
                <%if(shiro.hasPermission("sys:schedule:stop")){%>
                    <button id="stop" class="layui-btn layui-btn-normal">
                        <i class="layui-icon layui-icon-pause"></i> 暂停
                    </button>
                <%}%>
                <%if(shiro.hasPermission("sys:schedule:resume")){%>
                    <button id="resume" class="layui-btn layui-btn-normal">
                        <i class="layui-icon layui-icon-play"></i> 启动
                    </button>
                <%}%>
                <%if(shiro.hasPermission("sys:schedule:startnow")){%>
                    <button id="startNow" class="layui-btn layui-btn-normal">
                        <i class="layui-icon layui-icon-log"></i> 立即运行一次
                    </button>
                <%}%>
            </div>
            <%}%>
			<table id="scheduleList" lay-filter="scheduleList"></table>

			<!--操作-->
			<script type="text/html" id="scheduleListBar">
				<%if(shiro.hasPermission("sys:schedule:edit")){%>
				<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
				<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
				<%}%>
				<a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="look">预览</a>
                <%if(shiro.hasPermission("sys:schedule:stop")){%>
                <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="stop">暂停</a>
                <%}%>
                <%if(shiro.hasPermission("sys:schedule:resume")){%>
                <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="resume">启动</a>
                <%}%>
                <%if(shiro.hasPermission("sys:schedule:startnow")){%>
                <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="startnow">立即运行一次</a>
                <%}%>
			</script>
		</div>
	</form>
</div>

<script>
    layui.use(['form','layer','laydate','table','laytpl'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laydate = layui.laydate,
            laytpl = layui.laytpl,
            table = layui.table;

        //列表
        var tableIns = table.render({
            elem: '#scheduleList',
            url : '${ctx}/sys/schedule/listAjax',
            cellMinWidth : 95,
            page : true,
            request: {
                pageName: 'pageNo', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            limit : 15,
            limits : [10,15,20,25],
            id : "scheduleListTable",
            cols : [[
                {type: "checkbox", fixed:"left", width:50},
					{field:'name', title: '任务名'},
					{field:'tGroupLabel', title: '任务组'},
					{field:'expression', title: '定时规则'},
					{field:'statusLabel', title: '启用状态'},
					{field:'classname', title: '任务类'},
					{field:'description', title: '描述'},
					{field:'updateDate', title: '更新时间'},
				{title: '操作', width:170, templet:'#scheduleListBar',fixed:"right",align:"center"}

            ]]
        });

        //搜索
        form.on('submit(LAY-schedule-search)', function(data){
				var name = $('#name');
			table.reload("scheduleListTable",{
				page: {
					curr: 1 //重新从第 1 页开始
				},
				where: {
						name : name.val(),
				}
			})
        });

        //添加
        function addschedule(id){
            var index = layui.layer.open({
                title : "添加系统定时任务",
                type : 2,
                content : "${ctx}/sys/schedule/form?id="+id,
                area: ['90%', '80%'],
                <%if(shiro.hasPermission("sys:schedule:edit")){%>
                btn: ['确定', '取消'],
                yes: function(index, layero){
                    var submit = layero.find('iframe').contents().find('#addData');
                    submit.trigger('click');
                }
				<%}%>
            })
        }
        $(".addschedule_btn").click(function(){
            var id = '${schedule.id}';
            addschedule(id);
        })


        //批量删除
        $(".delAll_btn").click(function(){
            var checkStatus = table.checkStatus('scheduleListTable'),
                data = checkStatus.data,
                selectedIds = [];
            if(data.length > 0) {
                for (var i in data) {
                    selectedIds.push(data[i].id);
                }
                layer.confirm('确定删除选中的数据？', {icon: 3, title: '提示信息'}, function (index) {
                    $.post("${ctx}/sys/schedule/batchDelete",{selectedIds:selectedIds.join(",")},function(result){
                        if(result.code=="0"){
                            layer.msg(result.msg);
                        }else{
                            layer.msg("删除失败");
                        }
                        tableIns.reload();
                        layer.close(index);
                    });
                })
            }else{
                layer.msg("请选择需要删除的数据");
            }
        })

        //列表操作
        table.on('tool(scheduleList)', function(obj){
            var layEvent = obj.event,
                data = obj.data;

            if(layEvent === 'edit'){ //编辑
                addschedule(data.id);
            } else if(layEvent === 'del'){ //删除
                layer.confirm('确定删除选中数据？',{icon:3, title:'提示信息'},function(index){
                    $.post("${ctx}/sys/schedule/delete",{id:data.id},function(result){
                        if(result.code=="0"){
                            layer.msg("删除成功");
                        }else{
                            layer.msg("删除失败");
                        }
                        tableIns.reload();
                        layer.close(index);
                    });
                });
            } else if(layEvent === 'look'){ //预览
                addschedule(data.id);
            }
        });
        //暂停
        $("#stop").on("click", function(){
            var checkStatus = table.checkStatus('scheduleListTable'),
                data = checkStatus.data;
            if(data.length > 0) {
                if (data.length == 1){
                    var id = data[0].id;
                    layer.confirm('确定要暂停定时任务吗？', {icon: 3, title: '提示信息'}, function (index){
                        var loadindex = layer.load(2, 0.8);
                        $.post("${ctx}/sys/schedule/stop?id=" + id, function(data){
                            if(data.success){
                                tableIns.reload();
                            }
                            layer.close(loadindex);
                            layer.msg(data.msg);
                            layer.close(index);
                        })
                    })
                }else{
                    layer.alert("不能选择多条数据，请选择一条数据");
                }
            }else{
                layer.alert("请选择一条数据");
            }
        });

        //恢复
        $("#resume").on("click", function(){
            var checkStatus = table.checkStatus('scheduleListTable'),
                data = checkStatus.data;
            if(data.length > 0) {
                if (data.length == 1){
                    var id = data[0].id;
                    layer.confirm('确定要恢复定时任务吗？', {icon: 3, title: '提示信息'}, function (index){
                        var loadindex = layer.load(2, 0.8);
                        $.post("${ctx}/sys/schedule/resume?id=" + id, function(data){
                            if(data.success){
                                tableIns.reload();
                            }
                            layer.close(loadindex);
                            layer.msg(data.msg);
                            layer.close(index);
                        })
                    })
                }else{
                    layer.alert("不能选择多条数据，请选择一条数据");
                }
            }else{
                layer.alert("请选择一条数据");
            }
        });

        //立即运行一次
        $("#startNow").on("click", function(){
            var checkStatus = table.checkStatus('scheduleListTable'),
                data = checkStatus.data;
            if(data.length > 0) {
                if (data.length == 1){
                    var id = data[0].id;
                    layer.confirm('确定要立即执行一次定时任务吗？', {icon: 3, title: '提示信息'}, function (index){
                        var loadindex = layer.load(2, 0.8);
                        $.post("${ctx}/sys/schedule/startNow?id=" + id, function(data){
                            if(data.success){
                                tableIns.reload();
                            }
                            layer.close(loadindex);
                            layer.msg(data.msg);
                            layer.close(index);
                        })
                    })
                }else{
                    layer.alert("不能选择多条数据，请选择一条数据");
                }
            }else{
                layer.alert("请选择一条数据");
            }
        });
    })
</script>
<%}%>